package com.zhuhanpeng.cloud.file.control;

import com.obs.services.model.ObsObject;
import com.zhuhanpeng.cloud.file.entity.COSEntity;
import com.zhuhanpeng.cloud.file.entity.PtFileInfoEntity;
import com.zhuhanpeng.cloud.file.entity.SearchEntity;
import com.zhuhanpeng.cloud.file.service.FileService;
import com.zhuhanpeng.cloud.mybatis.entity.PtFileEntity;
import com.zhuhanpeng.cloud.mybatis.entity.PtSendShareEntity;
import com.zhuhanpeng.cloud.mybatis.flexigrid.PadingRstType;
import com.zhuhanpeng.cloud.mybatis.flexigrid.PagingEntity;
import com.zhuhanpeng.cloud.spring.control.BaseController;
import com.zhuhanpeng.cloud.spring.util.OBSUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Controller
@RequestMapping(value = "/file")
public class FileController extends BaseController {
    private Logger logger = LogManager.getLogger(FileController.class);
    @Resource
    private FileService fileService;


    @Secured("ROLE_cldk_data_file")
    @RequestMapping("/filePage")
    public String filePage() {
        return "file/filePage";
    }

    @RequestMapping("/queryFileInfoByPage")
    @ResponseBody
    public PadingRstType<PtFileInfoEntity> queryFileInfoByPage(PtFileInfoEntity entity, PagingEntity paging) {
        logger.info(entity);
        logger.info(paging);
        PadingRstType<PtFileInfoEntity> padingRstType = fileService.queryFileInfoByPage(entity, paging);
        return padingRstType;
    }

    //文件下载页面路由
    @RequestMapping("/FileUpload")
    public String load() {
        return "file/filePage";//filePage.html
    }

    //前端请求路由
    @RequestMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file, HttpSession Session, HttpServletRequest req) throws IOException {
        //将文件存入obs
        OBSUtils obs = new OBSUtils();
        String Key = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();
        obs.ObsUpload("ntlg-obs", Key, inputStream);
        return sucessJson("upload.info.success");
    }

    @RequestMapping("/fileDownLoadById")
    @ResponseBody
    public String fileDownLoadById(String fileName, HttpServletRequest req, HttpServletResponse rep) {
        //obs下载文件
        //后台收到前端穿过来的filename(文件名)
        OBSUtils obs = new OBSUtils();
        String Key = fileName;
        ObsObject obsObject = obs.getFile(Key);
        InputStream inputStream = obsObject.getObjectContent();
        obs.dowloadFile(rep, inputStream, fileName);
        return "file/filePage";//重定向到当前下载页面
    }

    @PostMapping("/updateFile")
    @ResponseBody
    public String updateFile(MultipartFile file) {
        fileService.uploadFileInfo(file);
        return sucessJson("upload.info.success");
    }

    @GetMapping("/queryAllFileInfoList")
    @ResponseBody
    public String queryAllFileInfoList(SearchEntity entity) {
        SearchEntity page = fileService.queryAllFileInfoList(entity);
        return successData(page);
    }

    @PostMapping("/downloadFile")
    @ResponseBody
    public String downloadFile(PtFileEntity fileEntity) {
        fileService.downloadFile(fileEntity.getFileName());
        return null;
    }

    @PostMapping("/deleteFileInfo")
    @ResponseBody
    public String deleteFileInfo(@RequestBody PtFileEntity fileEntity) {
        fileService.deleteFileInfo(fileEntity);
        return sucessJson("file.delete.info.success");
    }

    @PostMapping("/collectionFileInfo")
    @ResponseBody
    public String collectionFileInfo(@RequestBody PtFileEntity fileEntity) {
        fileService.collectionFileInfo(fileEntity);
        return sucessJson("file.collectionFileInfo.success");
    }

    @GetMapping("/queryCollectionInfo")
    @ResponseBody
    public String queryCollectionInfo(SearchEntity entity) {
        SearchEntity page = fileService.queryCollectionInfo(entity);
        return successData(page);
    }

    @PostMapping("/removeCollectionInfo")
    @ResponseBody
    public String removeCollectionInfo(@RequestBody PtFileEntity fileEntity) {
        fileService.removeCollectionInfo(fileEntity);
        return sucessJson("file.remove.collection.success");
    }

    @GetMapping("/queryRecycleFileInfo")
    @ResponseBody
    public String queryRecycleFileInfo(SearchEntity entity) {
        SearchEntity page = fileService.queryRecycleFileInfo(entity);
        return successData(page);
    }

    @PostMapping("/refreshFileInfo")
    @ResponseBody
    public String refreshFileInfo(@RequestBody PtFileEntity entity) {
        fileService.refreshFileInfo(entity);
        return sucessJson("file.refresh.info.success");
    }

    @PostMapping("/deleteRecycleFileInfo")
    @ResponseBody
    public String deleteRecycleFileInfo(@RequestBody PtFileEntity fileEntity) {
        fileService.deleteRecycleFileInfo(fileEntity);
        return sucessJson("file.delete.recycle.info.success");
    }

    @GetMapping("/getTencentCosInfo")
    @ResponseBody
    public String getTencentCosInfo() {
        COSEntity cos = fileService.getTencentCosInfo();
        return successData(cos);
    }

    @PostMapping("/shareFilesInfo")
    @ResponseBody
    public String shareFilesInfo(@RequestBody PtSendShareEntity entity) {
        fileService.shareFilesInfo(entity);
        return sucessJson("file.share.success");
    }

    @GetMapping("/queryShareFileInfoList")
    @ResponseBody
    public String queryShareFileInfoList(SearchEntity entity) {
        SearchEntity page = fileService.queryShareFileInfoList(entity);
        return successData(page);
    }

    @PostMapping("/deleteShareFileInfos")
    @ResponseBody
    public String deleteShareFileInfos(@RequestBody PtSendShareEntity entity) {
        fileService.deleteShareFileInfos(entity);
        return sucessJson("file.delete.share.info.success");
    }

    @PostMapping("/getFileInfo")
    @ResponseBody
    public String getFileInfo(@RequestBody PtSendShareEntity entity) {
        PtFileEntity ptFileEntity = fileService.getFileInfo(entity);
        return successData(ptFileEntity);
    }
}
